<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\TongCarousel;
use App\Admin\Repositories\TongGood;
use App\Models\TongGoodsGroup;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TongCarouselController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TongCarousel(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('c_img','轮播图')->gallery();
            $grid->column('status','状态')->switch();
            $grid->column('is_dump','是否跳转')->display(function($is_dump){
                if($is_dump==0){
                    return '无跳转';
                }else{
                    return '跳转';
                }
            });
            $grid->column('dump_url','跳转地址')->display(function($dump_url){
                if(empty($dump_url)){
                    return '#';
                }else{
                    return $dump_url;
                }
            });
            $grid->column('created_at','创建时间');
            $grid->column('updated_at','更新时间')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TongCarousel(), function (Show $show) {
            $show->field('id');
            $show->field('c_img');
            $show->field('status');
            $show->field('is_dump');
            $show->field('dump_url');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TongCarousel(), function (Form $form) {
            $form->display('id');
            $form->image('c_img','轮播图')->rules('required', [
                'required' => '请上传轮播图',
            ]);
            $form->radio('status','状态')->options(['0' => '下架', '1'=> '上架'])->default('0');
            $form->radio('is_dump','跳转')->options(['0' => '无跳转', '1'=> '跳转'])
                ->when(1, function (Form $form) {
                    $goods_model = \App\Models\TongGood::query()->where('goods_status',1)->pluck('goods_name','id')->toArray();
                    $form->select('dump_url','商品id')->options($goods_model)->rules('required_if:is_dump,1',['status.required_if'=>'请输入跳转的商品']);
                })
                ->default('0');
            //$form->text('is_dump');
            //$form->text('dump_url');
        });
    }
}
